package com.foreknow.demo3;

/**
 * 4、设计一个类Student，该类包括姓名、学号和成绩。设计一个方法，按照成绩从高到低的顺序输出姓名、学号和成绩信息。
 */
public class Student {
    private String name;
    private int id;
    private int score;

    public Student(){

    }

    public Student(String name,int id,int score) {
        this.name = name;
        this.id = id;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public void getSort(Student[] stu) {
        for(int j = 1;j < stu.length;j++){  //总的比较次数
            for(int i = 0;i<stu.length-j;i++){ //相邻的两个元素进行比较
                if(stu[i].getScore() < stu[i+1].getScore()){
                    Student temp = stu[i];
                    stu[i] = stu[i+1];
                    stu[i+1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        //创建一个数组容器
        Student[] students = new Student[3];
        //创建3个学生对象并将这些对象保存到数组中
        Student s1 = new Student("张三",101,90);
        Student s2 = new Student("张三",101,50);
        Student s3 = new Student("张三",101,80);
        students[0] = s1;
        students[1] = s2;
        students[2] = s3;
        //调用排序的方法
//        s1.getSort(students);
        //遍历输出结果
//        for(int i = 0;i < students.length;i++) {
//            //从数组中获取元素
//            Student s = students[i];
//            System.out.println(s.getName()+"同学的学号为:"+s.getId()+"成绩为:"+s.getScore()+"名次:"+(i+1));
//        }
    }
}
